#include "etcd.hpp"
#include "channel.hpp"
#include "transmite.pb.h"
#include "utils.hpp"
#include <gtest/gtest.h>
DEFINE_bool(run_mode, false, "程序的运行模式, false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "关注的服务名称");
ServiceChannel::ChannelPtr channel;
// TEST(消息转发测试, 测试1) {

//     chat_system::MessageContent message;
//     message.set_message_type(chat_system::MessageType::STRING);
//     message.mutable_string_message()->set_content("roboko的测试消息");
//     chat_system::NewMessageReq req;
//     req.set_request_id(uuid());
//     req.set_user_id("6919-9f03ea75-0000");
//     req.set_chat_session_id("session_roboko");
//     req.mutable_message()->CopyFrom(message);
//     chat_system::MsgTransmitService_Stub stub(channel.get());
//     chat_system::GetTransmitTargetRsp rsp;
//     brpc::Controller cntl;
//     stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success()); // 期望成功
//     ASSERT_TRUE(rsp.message().message_id().empty() == false);
//     ASSERT_TRUE(rsp.message().chat_session_id() == req.chat_session_id());
//     LOG_DEBUG("{}", rsp.message().timestamp());
//     LOG_DEBUG("消息转发测试成功: {}", req.request_id());
//     LOG_DEBUG("{}", rsp.message().sender().user_id());
//     LOG_DEBUG("{}", rsp.message().sender().nickname());
//     LOG_DEBUG("{}", rsp.message().sender().description());
//     LOG_DEBUG("{}", rsp.message().sender().phone());
//     LOG_DEBUG("{}", rsp.message().sender().avatar());
//     ASSERT_TRUE(rsp.message().message().message_type() == chat_system::MessageType::STRING);
//     LOG_DEBUG("{}", rsp.message().message().string_message().SerializeAsString());
//     for(auto& id : rsp.target_id_list()) {
//         LOG_DEBUG("转发目标ID: {}", id);
//     }
// }

void string_message(const std::string& uid, const std::string &sid, const std::string &msg) {
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat_system::MsgTransmitService_Stub stub(channel.get());
    chat_system::NewMessageReq req;
    chat_system::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(chat_system::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    for(auto& id : rsp.target_id_list()) {
        std::cout << "转发目标ID: " << id << std::endl;
    }
    std::cout << "-----------------" << std::endl;

}

void image_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat_system::MsgTransmitService_Stub stub(channel.get());
    chat_system::NewMessageReq req;
    chat_system::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(chat_system::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    for(auto& id : rsp.target_id_list()) {
        std::cout << "转发目标ID: " << id << std::endl;
    }
    std::cout << "-----------------" << std::endl;

}

void file_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat_system::MsgTransmitService_Stub stub(channel.get());
    chat_system::NewMessageReq req;
    chat_system::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(chat_system::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(msg);
    req.mutable_message()->mutable_file_message()->set_file_size(msg.size());
    req.mutable_message()->mutable_file_message()->set_file_name("测试文件.txt");

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    for(auto& id : rsp.target_id_list()) {
        std::cout << "转发目标ID: " << id << std::endl;
    }
    std::cout << "-----------------" << std::endl;

}

void speech_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat_system::MsgTransmitService_Stub stub(channel.get());
    chat_system::NewMessageReq req;
    chat_system::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(chat_system::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    for(auto& id : rsp.target_id_list()) {
        std::cout << "转发目标ID: " << id << std::endl;
    }
    std::cout << "-----------------" << std::endl;

}


int main(int argc, char* argv[]) {
    init_logger(FLAGS_log_file, int_log_level_map(FLAGS_log_level), FLAGS_run_mode);
    google::ParseCommandLineFlags(&argc, &argv, true);
    testing::InitGoogleTest();
    ServiceManager::ptr service_manager = std::make_shared<ServiceManager>();
    service_manager->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    //获取用户服务信道管理对象
    auto channel_manager = service_manager->choose(FLAGS_transmite_service);
    if(channel_manager == nullptr) {
        LOG_DEBUG("获取文件服务信道失败");
        return -1;
    }
    // 获取用户服务信道
    channel = channel_manager->choose();
    if(channel == nullptr) {
        LOG_DEBUG("获取用户服务信道失败");
        return -1;
    }

    string_message("6919-9f03ea75-0000", "7651-7470d73d-0000", "你在干什么?");
    string_message("e7b5-538ed478-0001", "7651-7470d73d-0000", "我在学习C++网络编程");
    string_message("6919-9f03ea75-0000", "7651-7470d73d-0000", "别卷了");
    string_message("e7b5-538ed478-0001", "7651-7470d73d-0000", "那咋了");

    image_message("6919-9f03ea75-0000", "7651-7470d73d-0000", "可爱的表情包");  
    speech_message("6919-9f03ea75-0000", "7651-7470d73d-0000", "友好交流的语音");
    file_message("e7b5-538ed478-0001", "7651-7470d73d-0000", "游戏文件");
    return 0;
}